home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / manual / examples / termios.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  1KB  |  61 lines

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <termios.h>
  5.  
  6. /* Use this variable to remember original terminal attributes. */
  7.  
  8. struct termios saved_attributes;
  9.  
  10. void 
  11. reset_input_mode (void)
  12. {
  13.   tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
  14. }
  15.  
  16. void 
  17. set_input_mode (void)
  18. {
  19.   struct termios tattr;
  20.   char *name;
  21.  
  22.   /* Make sure stdin is a terminal. */
  23.   if (!isatty (STDIN_FILENO))
  24.     {
  25.       fprintf (stderr, "Not a terminal.\n");
  26.       exit (EXIT_FAILURE);
  27.     }
  28.  
  29.   /* Save the terminal attributes so we can restore them later. */
  30.   tcgetattr (STDIN_FILENO, &saved_attributes);
  31.   atexit (reset_input_mode);
  32.  
  33. /*@group*/
  34.   /* Set the funny terminal modes. */
  35.   tcgetattr (STDIN_FILENO, &tattr);
  36.   tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO.  */
  37.   tattr.c_cc[VMIN] = 1;
  38.   tattr.c_cc[VTIME] = 0;
  39.   tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
  40. }
  41. /*@end group*/
  42.  
  43. int
  44. main (void)
  45. {
  46.   char c;
  47.  
  48.   set_input_mode ();
  49.  
  50.   while (1)
  51.     {
  52.       read (STDIN_FILENO, &c, 1);
  53.       if (c == '\004')        /* @kbd{C-d} */
  54.     break;
  55.       else
  56.     putchar (c);
  57.     }
  58.  
  59.   return EXIT_SUCCESS;
  60. }
  61.